package com.tao.com.xiaowen.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.tao.com.xiaowen.R;
import com.tao.com.xiaowen.model.Favorite;

import java.util.List;

/**
 * Created by tao on 17-2-24.
 */

public class MyFavoriteAdapter extends RecyclerView.Adapter<MyFavoriteAdapter.MyFavoriteViewHolder> {
    private Context context;
    private List<Favorite> favoriteList;
    private LayoutInflater inflater;

    private OnItemClickListener onItemClickListener;

    public MyFavoriteAdapter(Context context, List<Favorite> favoriteList) {
        this.context = context;
        this.favoriteList = favoriteList;
        inflater = LayoutInflater.from(context);
    }

    public void OnItemLongClick(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyFavoriteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.activity_favorite_item, null);
        MyFavoriteViewHolder holder = new MyFavoriteViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyFavoriteViewHolder holder, final int position) {
        final Favorite favorite = favoriteList.get(position);
        String picture = favorite.getPicture();
        holder.contentView.setText(favorite.getContent());
        if (TextUtils.isEmpty(picture)) {
            holder.pictureView.setVisibility(View.GONE);
        } else {
            holder.pictureView.setVisibility(View.VISIBLE);
            Glide.with(context).load(picture)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .placeholder(R.drawable.default_content_pic)
                    .error(R.drawable.default_content_pic)
                    .crossFade()
                    .into(holder.pictureView);
        }

        if (onItemClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int lp = holder.getLayoutPosition();
                    onItemClickListener.OnItemLongClick(v, lp);
                    return true;
                }
            });
        }
    }

    public void deleteData(int position) {
        favoriteList.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public int getItemCount() {
        return favoriteList.size();
    }


    class MyFavoriteViewHolder extends RecyclerView.ViewHolder {
        private TextView contentView;
        private ImageView pictureView;

        public MyFavoriteViewHolder(View itemView) {
            super(itemView);

            contentView = (TextView) itemView.findViewById(R.id.content);
            pictureView = (ImageView) itemView.findViewById(R.id.picUrl);
        }
    }

    public interface OnItemClickListener {
        void OnItemLongClick(View view, int position);
    }
}
